<?php
/**
 * User: @lose
 * Date: 2020-02-25
 * Time: 18:25
 */

namespace Frieza\Modules\UserGrowth\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\UserGrowth\RecommendOfficer as mRecommendOfficer;
use Doraemon\model\UserGrowth\Prize as mPrize;
use Doraemon\service\UserGrowth\Lottery;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Form;
use Illuminate\Support\MessageBag;
use Doraemon\service\User;
use App\Exceptions\InputErrorException;
use Doraemon\pockets\constInterface\UserGrowth\LotteryConf;

class LotteryController extends Controller
{
    public function index(Content $content, Request $request)
    {
        $perPage = $request->input('per_page', 10);
        $page = $request->input('page', 1);
        $tab = new Tab();
        $tab->add('奖品列表', $this->prizeList($page, $perPage), true);
        $tab->add('推荐官管理', $this->recommendOfficerList());
        $content->row($tab);
        return $content
            ->header('抽奖活动')
            ->description('装修大礼包中奖数量' . mPrize::count(['prize_id' => LotteryConf::PRIZE_GIFT_PACKAGE]))
            ->breadcrumb(['text' => '抽奖活动']);
    }

    public function prizeList($page, $perPage)
    {
        $list = mPrize::select('*', [
            'prize_id[!]' => LotteryConf::PRIZE_GIFT_PACKAGE,
            'ORDER' => ['prize_id' => 'ASC', 'id' => 'DESC'],
        ]);

        $uidList = array_column($list, 'uid');
        $userInfo = User::getBatchBasicUserInfo($uidList);
        $userInfo = array_column($userInfo, null, 'uid');

        $total = mPrize::count(['prize_id[!]' => LotteryConf::PRIZE_GIFT_PACKAGE]);

        $gridModel = new pGridDataModel($list, $total);

        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $userInfo) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableFilter();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->disableActions();

                $grid->column("id", 'ID');
                $grid->column("uid", '用户信息')->display(function () use($userInfo) {
                    return $this->uid . ' | ' . $userInfo[$this->uid]['nick'];
                });
                $grid->column("prize_id", '奖品')->display(function () {
                    return LotteryConf::PRIZE_CONF[$this->prize_id]['name'];
                });
                $grid->column("prize_channel", '中奖渠道')->display(function () {
                    return $this->prize_channel == 1 ? '站内' : '站外';
                });
                $grid->column("addtime", '中奖时间');
            });

        return $gridList->render();
    }

    public function recommendOfficerList()
    {
        $list = mRecommendOfficer::select('*');

        $uidList = array_column($list, 'uid');
        $userInfo = User::getBatchBasicUserInfo($uidList);
        $userInfo = array_column($userInfo, null, 'uid');

        $total = mRecommendOfficer::count();
        $gridModel = new pGridDataModel($list, $total);

        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $userInfo) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableFilter();
                $grid->disableRowSelector();
                $grid->disablePagination();

                $grid->column("id", 'ID');
                $grid->column("uid", '用户信息')->display(function () use($userInfo) {
                    return $this->uid . ' | ' . $userInfo[$this->uid]['nick'];
                });
                $grid->column("success_invite", '邀请好友数量');
                $grid->column("admin_id", '操作者')->display(function () {
                    return Admin::user()->find($this->admin_id)->name;
                });
                $grid->column("addtime", '添加时间');
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                });
            });
        return $gridList->render();
    }

    public function create(Content $content, $id = 0)
    {
        $data = [];
        if($id) {
            $data = mRecommendOfficer::getInfoById($id);
        }
        $form = new Form($data);
        $form->text('uid', 'uid')->required('不能为空');
        $form->text('success_invite', '邀请好友数量')->required('不能为空');
        $form->hidden('id');
        $form->action('/admin/userGrowth/lottery/save');

        return $content
            ->header('编辑推荐官')
            ->breadcrumb([
                'text' => '抽奖活动',
                'url' => '/userGrowth/lottery',
            ], [
                'text' => '编辑推荐官'
            ])->body($form->render());
    }

    public function save(Request $request)
    {
        $params = $request->all();

        $userInfo = User::getBasicUserInfo($params['uid']);
        if(!$userInfo) {
            throw new InputErrorException('uid异常', InputErrorException::DEFAULT_CODE, 'uid');
        }

        // 接收参数计算活动开始天数 用天数 * 3
        // 接收邀请成功的数量 用数量 * 3
        // 以上两者相加即是累计抽奖次数
        $result = Lottery::admAddRecommendOfficer($params, Admin::user()->id);
        if($result) {
            $success = new MessageBag([
                'title' => '编辑成功'
            ]);
            return redirect('/admin/userGrowth/lottery')->with(compact('success'));
        } else {
            return back()->with(compact('error'));
        }
    }
}